package com.cola.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cola.common.CookieConstant;
import com.cola.entity.User;
import com.cola.entity.WeChatInfo;
import com.cola.utils.CookieUtils;
import com.cola.utils.PropertyUtils;
import com.cola.utils.jedis.JedisCacheDB;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Max on 2017/8/17.
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware {

    public final static String  AGAIN="again";
    public final static String  SUBSCRIBE="subscribe";
    public final static String  SHOP_LOGIN="toShopLogin";
    public final static String  MEP_LOGIN="toMepLogin";
    public final static String  ERROR_500="error_500";
    public final static String  AUTH="auth";

    public final static String  SHOP_INDEX="shop_index";

    public final static String  MEP_INDEX="mep_index";

    public final static String  COURIER_INDEX="courier_index";

    private static final Logger logger = LoggerFactory.getLogger(BaseAction.class);
    private HttpServletRequest request;
    private HttpServletResponse response;


    /**
     * 获取当前的request对象
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * 获取当前的response对象
     *
     * @return
     */
    public HttpServletResponse getResponse() {
        return response;
    }

    //设置request对象
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    //设置response对象
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * 设置request参数
     *
     * @param name  参数名
     * @param value 参数值
     */
    public void setRequestAttribute(String name, Object value) {
        request.setAttribute(name, value);
    }

    /**
     * 设置SessionAttribute
     *
     * @param name  参数名
     * @param value 参数值
     */
    public void setSessionAttribute(String name, Object value) {
        request.getSession().setAttribute(name, value);
    }

    /**
     * 获取SessionAttribute
     *
     */
    public Object getSessionAttribute(String key) {
        return request.getSession().getAttribute(key);
    }


    /**
     * 设置错误信息
     * @param errorInfo
     */
    public void setErrorInfoToRequest(String errorInfo){
        setRequestAttribute("errorInfo",errorInfo);
    }


    //获取当前链接

    public static String getRequestUrl(HttpServletRequest request) {
        String url="";
        String domain= PropertyUtils.getProperty("domain");
        String requestUri=request.getRequestURI();
        if(requestUri.startsWith("//")){
            requestUri=requestUri.substring(1, requestUri.length());
        }
        logger.info("domain:"+domain+"|requestUri:"+requestUri);
        url=domain+requestUri;
        String queryStr=request.getQueryString();
        if(StringUtils.isNotEmpty(queryStr)){
            url=url +"?"+queryStr;
        }
        return url;
    }


    /**
     * 将用户信息存至Redis以及将RedisKey存入Cookies
     * @param user ,loginType
     * @return 
     * @author Max
     * @since 2017/9/12
     */
    public boolean saveUserInfoToRedisCookie(User user,String loginType){

        String redisKey = CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY+"_"+user.getWeChatInfo().getOpenid();

        String redisValue = JSON.toJSONString(user);

        boolean a = JedisCacheDB.saveObject(redisKey,redisValue,0);

        boolean b = CookieUtils.setCookie(getResponse(),CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY,redisKey);

        boolean c =  CookieUtils.setCookie(getResponse(),CookieConstant.COLA_WX_ORDERING_SYSTEM_LOGIN_TYPE,loginType);

        return a && b && c;
    }

    /**
     * 从Redis获取用户信息
     * @param openId
     * @return User
     * @author Max
     * @since 2017/9/12
     */
    public User getUserInfoFromRedis(){

        String redisKey = CookieUtils.getCookieValue(getRequest(),CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY);

        if(redisKey != null){
            Object redisValue = JedisCacheDB.getObject(redisKey);
            if(redisValue != null){
                return JSONObject.parseObject(redisValue.toString(),User.class);
            }
        }
        return null;
    }

    /**
     * 清空用户信息
     * @author Max
     * @since 2017/9/18
     */
    public boolean emptyUserInfo(){
        String redisKey = CookieUtils.getCookieValue(getRequest(),CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY);

        boolean c = JedisCacheDB.expireObject(redisKey,-1);
        boolean a = CookieUtils.setCookie(getResponse(),CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY,"",-1);
        boolean b = CookieUtils.setCookie(getResponse(),CookieConstant.COLA_ORDERING_SYSTEM_WX_INFO_KEY,"",-1);

        return a && b && c;
    }


    /**
     * 获取登录类型
     * @return String
     * @author Max
     * @since 2017/9/14
     */
    public String getLoginTypeFromCookie(){
        return CookieUtils.getCookieValue(getRequest(),CookieConstant.COLA_WX_ORDERING_SYSTEM_LOGIN_TYPE);
    }





    /**
     * 将用户微信信息存至Redis以及将RedisKey存入Cookies
     * @param user ,loginType
     * @return
     * @author Max
     * @since 2017/9/12
     */
    public boolean saveWeChatInfoToRedisCookie(WeChatInfo weChatInfo){

        String redisKey = CookieConstant.COLA_ORDERING_SYSTEM_WX_INFO_KEY+"_"+weChatInfo.getOpenid();

        String redisValue = JSON.toJSONString(weChatInfo);

        boolean a = JedisCacheDB.saveObject(redisKey,redisValue,0);

        boolean b = CookieUtils.setCookie(getResponse(),CookieConstant.COLA_ORDERING_SYSTEM_WX_INFO_KEY,redisKey);

        return a && b;
    }

    /**
     * 获取用户微信信息
     * @return Map<String,Object>
     * @author Max
     * @since 2017/9/13
     */
    public WeChatInfo getWeChatInfoFromRedis(){
        String redisKey = CookieUtils.getCookieValue(getRequest(),CookieConstant.COLA_ORDERING_SYSTEM_WX_INFO_KEY);
        if(redisKey != null){
            Object redisValue = JedisCacheDB.getObject(redisKey);
            if(redisValue != null){
                return JSONObject.parseObject(redisValue.toString(),WeChatInfo.class);
            }
        }
        return null;
    }


}